package models;

import java.util.*;

import javax.persistence.*;

import play.data.validation.*;
import play.db.jpa.*;

@Entity
public class Agent extends Model {

	@OneToMany
	public List<Stage> orgaStage;

	@Required
	public String nom;

	@ManyToOne
	public CISService cisService;

	@ManyToOne
	public StructureCoord structureCoord;

	@Required
	public String prenom;

	@Required
	public Date dateNais;

	@Required
	@ManyToOne
	public Grade grade;

	@Required
	@ManyToOne
	public Statut statut;

	@Email
	@Required
	public String email;

	@Required
	@Password
	public String password;

	@OneToMany(cascade=CascadeType.ALL)
	public List<Candidat> candidatures;

	@ManyToOne
	public NivFor niv;

	public Agent(String nom, String prenom, Date dateNais,CISService cisService,StructureCoord struct,
			Grade grade,Statut statut,String pass,String mail){
		this.nom = nom;
		this.prenom = prenom;
		this.dateNais = dateNais;
		this.cisService=cisService;
		this.structureCoord=struct;
		this.grade=grade;
		this.statut=statut;
		this.password=pass;
		this.email=mail;
		candidatures=new ArrayList<Candidat>();
	}

	public static Agent connect(String email, String password) {
		return find("byEmailAndPassword", email, password).first();
	}

	public String fullname(){
		return nom+" "+prenom;
	}

	public String toString(){
		return nom+" "+prenom;
	}

	public void addCandidature(Candidat candidat){
		candidatures.add(candidat);
	}

	public void setCandidatures(List<Candidat> candidatures) {
		this.candidatures = candidatures;
	}

	public List<Candidat> getCandidatures() {
		return candidatures;
	}

	public boolean equals(Agent a){
		return a.getEmail()==this.getEmail();
	}

	public CISService getCisService() {
		return cisService;
	}

	public void setCisService(CISService cisService) {
		this.cisService = cisService;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Agent> getAgents(Long id_cis){
		CISService cis = CISService.findById(id_cis);
		List<Agent> agents = Agent.find("cisService like ?", cis).fetch();
		return agents;
	}

	public boolean isOrgaStage(Agent agent) {
		if (!orgaStage.isEmpty()){
			return true;
		}
		else{
			return false;
		}
	}
}
